package com.springboot.learning.framework.web.page;


import java.util.List;

public class PaginationList {

	/**
	 * 每页显示的行数
	 */
	int pageSize = 10;

	/**
	 * 当前页码
	 */
	private int currentPage = 0;

	/**
	 * 满足条件的总记录数
	 */
	private int totalSize = 0;

	/**
	 * 是否必须获取totalSize
	 */
	private boolean requiredTotalSize = true;

	public List<?> collection = null; // 列表集合

	public PaginationList() {
	}

	public List<?> getCollection() {
		return this.collection;
	}

	/**
	 * 得到每页显示的行数
	 * 
	 * @return int 行数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页显示的行数
	 * 
	 * @param pageSize
	 *            行数
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 设置当前页码
	 * 
	 * @param currentPage
	 *            当前页码
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * 得到当前页码
	 * 
	 * @return int 当前页码
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 得到总页数
	 * 
	 * @return int 总页数
	 */
	public int getPageCount() {
		return (int) Math.ceil((double) this.getTotalSize() / (double) pageSize);
	}

	/**
	 * 设置记录集合
	 * 
	 * @param list
	 *            记录集合
	 */
	public void setCollection(List<?> collection) {
		this.collection = collection;
	}

	/**
	 * 获取当前页行数
	 * 
	 * @return
	 */
	public int getCurrentPageRowsCount() {
		int size = this.getTotalSize();
		int temp = getCurrentPage() * getPageSize() + 1;
		if (temp > size) {
			return 0;
		}
		int fromNum = temp >= size ? size : temp;
		temp = (getCurrentPage() + 1) * getPageSize();
		int toNum = temp >= size ? size : temp;
		return toNum - fromNum + 1;
	}

	/**
	 * 获取起点
	 * 
	 * @return
	 */
	public int getFrom() {
		if(this.getPageSize()==-1)
			return -1;
		return this.getPageSize() * this.getCurrentPage();
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getTotalSize() {
		return this.totalSize;
	}

	public boolean isRequiredTotalSize() {
		return requiredTotalSize;
	}

	public void setRequiredTotalSize(boolean requiredTotalSize) {
		this.requiredTotalSize = requiredTotalSize;
	}

}

